﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;

namespace WINBASE.Utils
{
    
    public static class StringExtensions
    {
        public static string Left(this string str, int length)
        {
            return str.Substring(0, length);
        }

        public static string Right(this string str, int length)
        {
            return str.Substring(str.Length - length, length);
        }

        public static string ToString(this List<string> str, string Delimitor)
        {
            string strResult = "";
            foreach (string strValue in str)
            {
                strResult += (strResult == "" ? "" : Delimitor) + strValue;
            }
            return strResult;
        }

        public static string ToString(this List<string> str, string Delimitor, string Prefix, string Suffix)
        {
            string strResult = "";
            foreach (string strValue in str)
            {
                strResult += Convert.ToString((string.IsNullOrEmpty(strResult) ? "" : Delimitor)) + Prefix + strValue + Suffix;
            }
            return strResult;
        }
    }

    public static class DateTimeExtensions
    {
        public static DateTime ToDateTime(this string s)
        {
            try
            {
                return ToDateTime(s,CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern, CultureInfo.CurrentUICulture);
            }
            catch (FormatException)
            {
                throw;
            }
            catch (CultureNotFoundException)
            {
                throw; // Given Culture is not supported culture
            }
        }

        public static DateTime ToDateTime(this string s,string format, CultureInfo culture)
        {
            try
            {
                var r = DateTime.ParseExact(s: s,
                                            format: format,
                                            provider: culture);
                return r;
            }
            catch (FormatException)
            {
                throw;
            }
            catch (CultureNotFoundException)
            {
                throw; // Given Culture is not supported culture
            }
        }
    }

    public class ContainsSwitch
    {
        List<ContainsSwitch> actionList = new List<ContainsSwitch>();
        public string Value { get; set; }
        public Action Action { get; set; }
        public bool SingleCaseExecution { get; set; }
        public void Perform(string target)
        {
            foreach (ContainsSwitch act in actionList)
            {
                if (target.Contains(act.Value))
                {
                    act.Action();
                    if (SingleCaseExecution)
                        break;
                }
            }
        }
        public void AddCase(string value, Action act)
        {
            actionList.Add(new ContainsSwitch() { Action = act, Value = value });
        }
    }
}